package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.mapper.AdminMapper;
import com.biu.billiards.model.bo.HardwareBO;
import com.biu.billiards.model.bo.ShopBO;
import com.biu.billiards.model.entity.Hardware;
import com.biu.billiards.mapper.HardwareMapper;
import com.biu.billiards.model.entity.Shop;
import com.biu.billiards.model.entity.User;
import com.biu.billiards.model.vo.hardware.AddHardwareVo;
import com.biu.billiards.model.vo.hardware.GetHardwareVo;
import com.biu.billiards.model.vo.hardware.UpdateHardwareVO;
import com.biu.billiards.service.IHardwareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-11-11
 */
@Service
public class HardwareServiceImpl extends ServiceImpl<HardwareMapper, Hardware> implements IHardwareService {


    @Resource
    private HardwareMapper hardwareMapper;


//    @Override
//    public Hardware getHardware(String deviceId) {
//        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("device_id",deviceId);
//        return hardwareMapper.selectOne(queryWrapper);
//    }



        @Override
    public Hardware getHardware(String deviceId) {

        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pub",deviceId);



        return hardwareMapper.selectOne(queryWrapper);

    }

//    @Override
//    public Hardware getSubHardware(Integer shopId) {
//        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("shop_id",shopId);
//
//        Hardware hardware = hardwareMapper.selectOne(queryWrapper);
//
//        return hardware;
//    }

    @Override
    public String getSubHardware(String deviceId) {
        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",deviceId);
        Hardware hardware = hardwareMapper.selectOne(queryWrapper);

        return hardware!=null ? hardware.getSub():null;
    }





    @Override
    public Integer getShopId(String id) {

        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pub",id);
        Hardware hardware = hardwareMapper.selectOne(queryWrapper);

        return hardware.getShopId();
    }

    @Override
    public IPage<HardwareBO> getHardwarePage(GetHardwareVo getHardwareVo) {
        QueryWrapper<Hardware> query = new QueryWrapper<>();
        //新建分页条件
        Page<Hardware> p = new Page<>(getHardwareVo.getCurrentPage(),getHardwareVo.getPageSize());
        IPage<Hardware> userIPage = hardwareMapper.selectPage(p,query);
        IPage<HardwareBO> page = new Page<>();
        List<HardwareBO> adminDetailBOList = new ArrayList<>();
        for (Hardware admin: userIPage.getRecords()){
            HardwareBO adminDetailBO = new HardwareBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;
    }

    @Override
    public Boolean updateHardware(UpdateHardwareVO updateHardwareVO) {
        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hardware_id",updateHardwareVO.getHardwareId());
        Hardware hardware = hardwareMapper.selectOne(queryWrapper);

        BeanUtils.copyProperties(updateHardwareVO,hardware);

        return hardwareMapper.update(hardware, queryWrapper) == 1;
    }

    @Override
    public Boolean addHardware(AddHardwareVo addHardwareVo) {
        Hardware hardware = new Hardware();
        BeanUtils.copyProperties(addHardwareVo,hardware);

        return hardwareMapper.insert(hardware) == 1;
    }

    @Override
    public Boolean deleteHardware(Integer hardwareId) {
        QueryWrapper<Hardware> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hardware_id",hardwareId);
        return hardwareMapper.delete(queryWrapper) == 1;
    }
}
